<?php
/**
 * 订单发票
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/22
 * Time: 14:11
 */

namespace Resource\Service;
use Think\Model;

class OrderBillService extends Model
{
    protected $trueTableName = 'wz_order_bill';
    /**
     * @param $params
     * @return array|bool
     */
    public function addOrderBill($params) {
        $params['create_time'] = time();
        $params['update_time'] = time();
        $id = D('Resource/OrderBill')->addOrderBill($params);
        if($id) {
            return $id;
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getOrderBillInfo($params, $fields='') {
        $dataInfo = D('Resource/OrderBill')->getOrderBillInfo($params,$fields);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchOrderBillList($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        isset($params['status']) && $data['status'] =$params['status'];
        isset($params['admin_id']) && $data['admin_id'] =$params['admin_id'];
        isset($params['company_name']) && $data['company_title'] =['like',"%{$params['company_name']}%"];
        isset($params['admin_name']) && $data['admin_name'] =['like',"%{$params['admin_name']}%"];
        isset($params['type']) && $data['type'] =$params['type'];
        isset($params['department_name']) && $data['d.name'] =['like',"%{$params['department_name']}%"];
        isset($params['department_id']) && $data['department_id'] = $params['department_id'];

        //查询总数
        $total = D('Resource/OrderBill')->getOrderBillCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询列表
        $dataList = D('Resource/OrderBill')->getOrderBillList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['dataList'] = $dataList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateOrderBillInfo($params) {
        return D('Resource/OrderBill')->updateOrderBillInfo($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getAllOrderBillList($params){
        $dataInfo = D('Resource/OrderBill')->getAllOrderBillList($params);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return bool
     */
    public function deleteOrderBill($params) {
        return D('Resource/OrderBill')->deleteOrderBill($params);
    }
}